<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Support\Facades\DB;

class GankaoService
{
    /**
     * 调整套餐
     * @param $param
     * @return false|string
     */
    public static function editCardInfo($param){

        try {

            $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
            if(empty($admin)){
                return ToolUtil::returnJson(300,'用户编号错误');
            }
            if($admin['group_id'] !== 1){
                return ToolUtil::returnJson(300,'没有权限');
            }

            $sn_data = DB::table('t_sn_uid')->where(['sn'=>$param['sn']])->first(['card_info']);
            if(empty($sn_data) || empty($sn_data['card_info'])){
                return ToolUtil::returnJson(300,'没有赶考信息');
            }

            $card_info = json_decode($sn_data['card_info'],true);
            if(empty($card_info[0]['specification_id'])){
                return ToolUtil::returnJson(300,'赶考信息json格式错误');
            }

            $param['remark'] = '赶考调整套餐';
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$param['sn'],'card_info'=>$param['card_info']]);
            LogUtil::addAdminLog($param);

            $param['contentType'] = 41;
            $param['batch'] = array($param['sn']);
            $param['type'] = 2;
            $param['statusType'] = '赶考调整套餐';
            $push_data = ['taocan'=>$param['card_info'],'old_card_info'=>$card_info];
            $jPush = LogUtil::addPushLog($param,['edit_card_info'=>$push_data]);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'推送成功，等待平板端调整');
            }
            return ToolUtil::returnJson(200,'推送失败');

        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'网络拥堵，请稍后再试');
        } 

    }

    /**
     * 取消套餐
     * @param $param
     * @return false|string
     */
    public static function cancelCardInfo($param){

        try {

            $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
            if(empty($admin)){
                return ToolUtil::returnJson(300,'用户编号错误');
            }
            if($admin['group_id'] !== 1){
                return ToolUtil::returnJson(300,'没有权限');
            }

            $sn_data = DB::table('t_sn_uid')->where(['sn'=>$param['sn']])->first(['card_info']);
            if(empty($sn_data) || empty($sn_data['card_info'])){
                return ToolUtil::returnJson(300,'没有赶考信息');
            }
            $card_info = json_decode($sn_data['card_info'],true);
            if(empty($card_info[0]['specification_id'])){
                return ToolUtil::returnJson(300,'赶考信息json格式错误');
            }

            $param['remark'] = '赶考取消套餐';
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$param['sn'],'card_info'=>$sn_data['card_info']]);
            LogUtil::addAdminLog($param);

            $param['contentType'] = 42;
            $param['batch'] = array($param['sn']);
            $param['type'] = 2;
            $param['statusType'] = '赶考取消套餐';
            
            $jPush = LogUtil::addPushLog($param,['delete_card_info'=>$card_info]);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'推送成功，等待平板端取消');
            }
            return ToolUtil::returnJson(200,'推送失败');

        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'网络拥堵，请稍后再试');
        } 

    }

    /**
     * 升级学段
     * @param $param
     * @return false|string
     */
    public static function upgrade($param){

        try {

            $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
            if(empty($admin)){
                return ToolUtil::returnJson(300,'用户编号错误');
            }
            if($admin['group_id'] !== 1){
                return ToolUtil::returnJson(300,'没有权限');
            }

            $sn_data = DB::table('t_sn_uid')->where(['sn'=>$param['sn']])->first(['card_info']);
            if(empty($sn_data) || empty($sn_data['card_info'])){
                return ToolUtil::returnJson(300,'没有赶考信息');
            }
            $card_info = json_decode($sn_data['card_info'],true);
            if(empty($card_info[0]['card_info'])){
                return ToolUtil::returnJson(300,'赶考信息json格式错误');
            }

            foreach ($card_info as $key => $value) {
                if($value['card_info'] === '高中3年套装'){
                    return ToolUtil::returnJson(300,'已有高中3年套装，不能升级');
                }
            }

            $param['remark'] = '赶考升级学段';
            $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$param['sn'],'card_info'=>$sn_data['card_info']]);
            LogUtil::addAdminLog($param);

            $param['contentType'] = 44;
            $param['batch'] = array($param['sn']);
            $param['type'] = 2;
            $param['statusType'] = '赶考升级学段';
            
            $jPush = LogUtil::addPushLog($param,['upgrade_card_info'=>$card_info]);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'推送成功，等待平板端升级');
            }
            return ToolUtil::returnJson(200,'推送失败');

        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'网络拥堵，请稍后再试');
        } 

    }


}